#ifndef TRANSCODE_H
#define TRANSCODE_H
#include <vector>
extern "C"
{
    #include <libavcodec/avcodec.h>
    #include <libavformat/avformat.h>
    #include <libavutil/time.h>
    #include <libavutil/pixfmt.h>
    #include <libswscale/swscale.h>
    #include <libswresample/swresample.h>
    #include <libavutil/imgutils.h>

    #include <SDL.h>
    #include <SDL_audio.h>
    #include <SDL_types.h>
    #include <SDL_name.h>
    #include <SDL_main.h>
    #include <SDL_config.h>
}
#include "string"
#include "EventHandle/VideoPlayerEventHandle.h"
class Transcode
{
public:
    Transcode(std::string srcUrl, std::string desUrl,int width=1080,int height=720,int bit_rate=9000000,int fps=30,bool video=true,bool audio=true);

    void releaseSources();
    bool openFile();
    AVFrame* get_video_frame(enum AVPixelFormat pixfmt,int width,int height);
    bool add_video_stream();
    bool add_audio_stream();
    void doDecodeVideo(AVPacket *inpacket);
    void  doDecodeAudio(AVPacket *packet);
    void doTranscode();
    void  doEncodeVideo(AVFrame *frame);
    void  doEncodeAudio(AVFrame *frame);
    void  doWrite(AVPacket *packet,bool isVideo);
    AVFrame*  get_audio_frame(enum AVSampleFormat smpfmt,int64_t ch_layout,int sample_rate,int nb_samples);

    void setVideoCompressCallBack(VideoCompressCallBack *pointer){videoCompressCallBack=pointer;}
private:
     std::string srcPath="";
    std::string dstPath="";

    AVFormatContext *inFmtCtx = NULL,*ouFmtCtx = NULL;
    VideoCompressCallBack *videoCompressCallBack= nullptr;

//输入文件编码
    AVCodecID src_video_id;
    AVCodecID src_audio_id;

    int video_in_stream_index = -1,audio_in_stream_index = -1;
    int video_ou_stream_index = -1,audio_ou_stream_index = -1;


    AVCodecContext *video_en_ctx=NULL;
    AVCodecContext *video_de_ctx=NULL;
    AVCodecContext *audio_en_ctx=NULL;
    AVCodecContext *audio_de_ctx=NULL;

    struct SwrContext *swr_ctx=NULL;
    AVFrame *video_de_frame=NULL;



      //设置输出视频编码
    AVCodecID dst_video_id = AV_CODEC_ID_MPEG4;
     int  dst_width=1280;
      int  dst_height = 720;
      int dst_video_bit_rate = 9000000;       // 0.9Mb/s
      AVPixelFormat dst_pix_fmt = AV_PIX_FMT_NV12;
     int  dst_fps = 50;  //fps
      bool video_need_transcode = true;

       // 音频
     AVCodecID  dst_audio_id =  AV_CODEC_ID_MP3;
     int  dst_audio_bit_rate = 128*1000;
     int  dst_sample_rate = 44100;    // 44.1khz
      AVSampleFormat dst_sample_fmt =AV_SAMPLE_FMT_FLTP;
     int  dst_channel_layout = AV_CH_LAYOUT_STEREO;   // 双声道
      bool audio_need_transcode = true;
 //是否需要格式转换
     bool video_need_convert=false;
     bool audio_need_convert=false;



      AVFrame*  video_en_frame=NULL;
      AVFrame* audio_en_frame=NULL;
      SwsContext * sws_ctx=NULL;
      AVFrame *audio_de_frame=NULL;

      int64_t video_pts=0;
     int64_t audio_pts=0;
     int last_video_pts;
     int last_audio_pts;

     std::vector<AVPacket *> videoCache;
     std::vector<AVPacket *> audioCache;

};

#endif // TRANSCODE_H
